package com.go.where.manager.action.go;

import java.io.File;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;

import com.go.where.manager.action.SuperAction;
import com.go.where.manager.common.Constants;
import com.go.where.manager.freemarker.CustomFreemarkerManager;
import com.go.where.manager.page.Pagination;
import com.go.where.manager.pojo.BigClassInfo;
import com.go.where.manager.pojo.user.OperatorInfo;
import com.go.where.manager.service.BigClassService;
import com.go.where.manager.service.system.OperatorService;
import com.go.where.manager.util.CommonUtil;
import com.go.where.manager.util.KindEditUtil;

@InterceptorRefs( {
		@InterceptorRef(value = "defaultStack", params = { "maximumSize",
				"2048000", "allowedTypes",
				"image/bmp,image/png,image/gif,image/jpeg" }),
		@InterceptorRef(value = "defalutStackSet") })
public class BigClassAction extends SuperAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4383195694100314090L;
	private static final Logger log = Logger.getLogger(BigClassAction.class);

	public Pagination<?> page;

	@Resource(name = "bigClassService")
	private BigClassService bigClassService;
	@Resource
	private OperatorService operatorService;

	private BigClassInfo bigClassInfo;
	private BigClassInfo bigClassInfoQuery;

	private String classId;
	private String top;
	private String hot;

	private File icon;// 上传的文件
	private String iconFileName; // 文件名称
	private String iconContentType; // 文件类型

	/**
	 * 查询列表
	 * 
	 * @return
	 */
	public String list() {
		if (bigClassInfoQuery == null) {
			bigClassInfoQuery = new BigClassInfo();
			bigClassInfoQuery.setPageSize(Constants.INIT_PAGESIZE);
			bigClassInfoQuery.setCurrentPage(Constants.INIT_CURRENTPAGE);
		}
		try {
			String userId = getCurrentOperator().getUserId();
			OperatorInfo op = operatorService.queryOperatorInfoByUserId(userId);
			if (op != null) {
				String roleId = op.getRoleId();
				if (CustomFreemarkerManager.getValue("system.merchant.role.id")
						.equals(roleId)) {
					bigClassInfoQuery.setMroleId(roleId);
				}
			}
			bigClassInfoQuery.setIsDelete(Constants.NOT_DELETED);
			page = bigClassService.queryPage(bigClassInfoQuery);
		} catch (Exception e) {
			addActionError(e.getMessage());
			log.error("查询信息异常", e);
		}
		return LIST;
	}

	public String toEdit() {
		bigClassInfo = bigClassService.queryByPrimartKey(classId);
		return INPUT;
	}

	public String add() {
		return INPUT;
	}

	/**
	 * 保存
	 * 
	 * @return
	 */
	public String save() {
		try {
			String iconRealpath = ServletActionContext.getServletContext()
					.getRealPath(
							CommonUtil.FILE_SEPARATOR
									+ Constants.BIG_CLASS_ICON_IMAGES);
			String iconPath = KindEditUtil.uploadPicture(icon, iconFileName,
					iconRealpath, Constants.BIG_CLASS_ICON_IMAGES);
			bigClassInfo.setIconPath(iconPath);
			if (bigClassInfo != null) {
				if (StringUtils.isBlank(bigClassInfo.getIsHot())) {
					bigClassInfo.setIsHot("0");
				}
				if (StringUtils.isBlank(bigClassInfo.getIsTop())) {
					bigClassInfo.setIsTop("0");
				}
			}
			bigClassService.save(bigClassInfo, getCurrentOperator());
			redirectionUrl = "big_class!list.htm";
			return SUCCESS;
		} catch (Exception e) {
			log.error("保存信息异常", e);
			addActionError("保存信息异常！" + e);
			return this.add();
		}
	}

	/**
	 * 编辑
	 * 
	 * @return
	 */
	public String edit() {
		try {
			if (icon != null) {
				String iconRealpath = ServletActionContext.getServletContext()
						.getRealPath(
								CommonUtil.FILE_SEPARATOR
										+ Constants.BIG_CLASS_ICON_IMAGES);
				String iconPath = KindEditUtil.uploadPicture(icon,
						iconFileName, iconRealpath,
						Constants.BIG_CLASS_ICON_IMAGES);
				bigClassInfo.setIconPath(iconPath);
			}
			if (bigClassInfo != null) {
				if (StringUtils.isBlank(bigClassInfo.getIsHot())) {
					bigClassInfo.setIsHot("0");
				}
				if (StringUtils.isBlank(bigClassInfo.getIsTop())) {
					bigClassInfo.setIsTop("0");
				}
			}
			bigClassService.update(bigClassInfo, getCurrentOperator());
			redirectionUrl = "big_class!list.htm";
			return SUCCESS;
		} catch (Exception e) {
			log.error("修改信息异常", e);
			addActionError("修改信息异常！" + e);
			classId = bigClassInfo.getClassId();
			return this.toEdit();
		}
	}

	/**
	 * 详情
	 * 
	 * @return
	 */
	public String detail() {
		bigClassInfo = bigClassService.queryByPrimartKey(classId);
		return "detail";
	}

	/**
	 * 设置置顶或取消
	 * 
	 * @return
	 */
	public String setTop() {
		bigClassInfo = new BigClassInfo();
		bigClassInfo.setClassId(classId);
		bigClassInfo.setIsTop(top);
		try {
			bigClassService.update(bigClassInfo, getCurrentOperator());
			redirectionUrl = "big_class!list.htm";
			return SUCCESS;
		} catch (Exception e) {
			log.error("置顶信息设置异常", e);
			addActionError("置顶信息设置异常" + e);
			return this.list();
		}
	}

	/**
	 * 设置热门或取消
	 * 
	 * @return
	 */
	public String setHot() {
		bigClassInfo = new BigClassInfo();
		bigClassInfo.setClassId(classId);
		bigClassInfo.setIsHot(hot);
		try {
			bigClassService.update(bigClassInfo, getCurrentOperator());
			redirectionUrl = "big_class!list.htm";
			return SUCCESS;
		} catch (Exception e) {
			log.error("热门信息设置异常", e);
			addActionError("热门信息设置异常" + e);
			return this.list();
		}
	}

	public String falseDelete() {
		bigClassInfo = new BigClassInfo();
		bigClassInfo.setClassId(classId);
		bigClassInfo.setIsDelete(Constants.DELETED);
		
		try {
			bigClassService.update(bigClassInfo, getCurrentOperator());
			redirectionUrl = "big_class!list.htm";
			return SUCCESS;
		} catch (Exception e) {
			log.error("删除信息异常", e);
			addActionError("删除信息异常" + e);
			return this.list();
		}
	}

	public BigClassInfo getBigClassInfo() {
		return bigClassInfo;
	}

	public void setBigClassInfo(BigClassInfo bigClassInfo) {
		this.bigClassInfo = bigClassInfo;
	}

	public BigClassInfo getBigClassInfoQuery() {
		return bigClassInfoQuery;
	}

	public void setBigClassInfoQuery(BigClassInfo bigClassInfoQuery) {
		this.bigClassInfoQuery = bigClassInfoQuery;
	}

	public String getClassId() {
		return classId;
	}

	public void setClassId(String classId) {
		this.classId = classId;
	}

	public Pagination<?> getPage() {
		return page;
	}

	public void setPage(Pagination<?> page) {
		this.page = page;
	}

	public String getTop() {
		return top;
	}

	public void setTop(String top) {
		this.top = top;
	}

	public String getHot() {
		return hot;
	}

	public void setHot(String hot) {
		this.hot = hot;
	}

	public File getIcon() {
		return icon;
	}

	public void setIcon(File icon) {
		this.icon = icon;
	}

	public String getIconFileName() {
		return iconFileName;
	}

	public void setIconFileName(String iconFileName) {
		this.iconFileName = iconFileName;
	}

	public String getIconContentType() {
		return iconContentType;
	}

	public void setIconContentType(String iconContentType) {
		this.iconContentType = iconContentType;
	}

}
